package jwuo.myServ;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class LogManager
{
	String _fileName;
	String _tagLog;
	
	public LogManager(String aFileName, String aTagLog)
	{
		this._fileName = aFileName;
		this._tagLog = aTagLog;
	}
	
	public void appendLog(String text)
	{
		String lineToLog = GetLogLine(text);
	    File logFile = new File("sdcard/" + _fileName + ".file");
	    if (!logFile.exists())
	    {
	       try
	       {
	          logFile.createNewFile();
	       } 
	       catch (IOException e)
	       {
	          // TODO Auto-generated catch block
	          e.printStackTrace();
	       }
	    }
	    try
	    {
	       //BufferedWriter for performance, true to set append to file flag
	       BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); 
	       buf.append(lineToLog);
	       buf.newLine();
	       buf.close();
	    }
	    catch (IOException e)
	    {
	       // TODO Auto-generated catch block
	       e.printStackTrace();
	    }
	}
	
	private String GetLogLine(String text)
	{
		return GetDataTime() + "[" + _tagLog + "] - " + text; 
	}

	private String GetDataTime()
	{
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date date = new Date();
		return dateFormat.format(date);
	}

}
